package org.balthie.demo.jdk.util.concurrent.auxiliary.phaser;

import java.io.IOException;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
//通过Phaser控制任务的执行轮数
public class PhaserTest3 {
	@Test
	public void test() throws IOException, InterruptedException {

		int repeats = 3; // 指定任务最多执行的次数

		Phaser phaser = new Phaser() {
			@Override
			protected boolean onAdvance(int phase, int registeredParties) {
				System.out.println(
						"---------------PHASE[" + phase + "],Parties[" + registeredParties + "] ---------------");
				return phase + 1 >= repeats || registeredParties == 0;
			}
		};

		for (int i = 0; i < 10; i++) {
			phaser.register(); // 注册各个参与者线程
			new Thread(new Task3(phaser), "Thread-" + i).start();
		}
		
		TimeUnit.SECONDS.sleep(10);
	}
}

class Task3 implements Runnable {
    private final Phaser phaser;

    Task3(Phaser phaser) {
        this.phaser = phaser;
    }

    @Override
    public void run() {
        while (!phaser.isTerminated()) {   //只要Phaser没有终止, 各个线程的任务就会一直执行
//        	模拟任务耗时
        	try {
				TimeUnit.MILLISECONDS.sleep((long) (Math.random()* 1000));
			} catch (InterruptedException e) {
			}
        	
        	System.out.println(Thread.currentThread().getName() + ": 执行完任务 arrivedAndAwait");
            int i = phaser.arriveAndAwaitAdvance();     // 等待其它参与者线程到达
            // do something
            System.out.println(Thread.currentThread().getName() + ": advance " + i);
        }
    }
}
